<?php
//include_once('class-profiler.php');

class COptions{

	private static $instance = null;
	
	function __construct(){
		add_action('admin_menu', array($this,'test_add_theme_page'));		
		add_action('wp_ajax_render_chart', array($this,'render_chart'));
	}

	function test_add_theme_page() {
		if ( isset( $_GET['page'] ) && $_GET['page'] == 'vp_options' ) {
	
			add_action('admin_head', array($this,'do_ajax'));
		}
	        add_options_page('Vote Profiler Options', 'Vote Profiler', 'manage_options', 'vp_options', array($this,'do_page'));

	}

	public function get_instance(){
		if(is_null(self::$instance)) {
			self::$instance = new COptions();  
		}
 		return self::$instance;
	}

	function do_ajax() {
	?>
	    <script type="text/javascript">
		jQuery(document).ready(function($) {
			jQuery.ajaxSetup({cache:false});
			jQuery('a').click(function(){
			  var data = {
				action: 'render_chart',
				whatever: jQuery(this).attr("rel")
			  };
			  jQuery("#test").html("loading...");
			  jQuery("#test").load(ajaxurl,data,function(response) {
				alert('Got this from the server: ' + response);
			  });
			  return false;
		  });
	
		});
		</script>
	
	<?php
	}
	
	// Draw the menu page itself
	function do_page() {
	?>
	<div class="wrap">
		<h2><?php _e('Test Admin'); ?></h2>
	
		<a href ="" rel="_reaction_buttons_OLD2415">Chart</a>
		<div id="test"></div>
		<div id="saved"></div>
	</div>
	<?php
	}
	
	function render_chart() {
		global $wpdb; // this is how you get access to the database
	
		$whatever = $_POST['whatever'];
	
		//$whatever += 10;
	
	        echo $whatever;
	
		die(); // this is required to return a proper result
	}	

}
?>